package com.yq.master.controller;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yq.core.base.service.BaseService;
import com.yq.core.entity.SysDepartmentEntity;
import com.yq.core.entity.SysRoleEntity;
import com.yq.core.service.SysDepartmentService;
import com.yq.core.service.SysRoleService;
import com.yq.core.util.Page;
import com.yq.core.util.Result;
import com.yq.master.annotation.AzerothAutowired;
import com.yq.master.bean.WebUserBean;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 信  息: [yinquan] [2017-03-04] [1.2];
 * 描  述: 信息状态返回类，同意使用此类返回数据
 */
@Controller
@RequestMapping(value = "/sys/department")
public class SysDepartmentController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(SysDepartmentController.class);

    @AzerothAutowired
    public SysDepartmentService sysDepartmentService;

    protected BaseService getServcie() {
        return sysDepartmentService;
    }

    protected String getJumpData() {
        return "";
    }

    /*****************************************************************************************/
    /** 添加部门初始化 **/
    @RequestMapping(value = "insert",params="_m=init")
    public String insertInit(Model model, ServletRequest request) {
        return "sys/department/sys-dep-insert";
    }

    /** 添加部门 **/
    @RequestMapping(value = "insert",params="_m=exec")
    @ResponseBody
    public Object insertExec(Model model, ServletRequest request) {
        super.fieldMap.clear();
        super.fieldMap.put("useStatus", "1");// 0禁用，1启用
        return super.insertExec(model, request);
    }

    /*****************************************************************************************/
    /** 编辑部门初始化 **/
    @RequestMapping(value = "update",params="_m=init")
    public String updateInit(Model model, ServletRequest request) {
        String uid = request.getParameter("uid");
        if(StringUtils.isNotBlank(uid)) {
            // 编辑初始化页面
            SysDepartmentEntity sysDepartmentEntity = sysDepartmentService.findUniqueById(uid);
            model.addAttribute("obj", sysDepartmentEntity);
        }
        return "sys/department/sys-dep-update";
    }

    /** 编辑部门 **/
    @RequestMapping(value = "update",params="_m=exec")
    @ResponseBody
    public Object updateExec(Model model, ServletRequest request) {
        //super.fieldMap.clear();
        return super.updateExec(model, request);
    }

    /*****************************************************************************************/
    /** 删除部门 **/
    @RequestMapping(value = "delete",params="_m=exec")
    @ResponseBody
    public Object deleteExec(Model model, ServletRequest request) {
        super.fieldMap.clear();
        super.fieldMap.put("deleteStatus", "1");
        return super.updateExec(model, request);
    }

    /*****************************************************************************************/
    /** 角色搜索初始化 **/
    @RequestMapping(value = "search",params="_m=init")
    public String searchInit(Model model, ServletRequest request) {
        return "sys/department/sys-dep-search";
    }

    /** 角色搜索 **/
    @RequestMapping(value = "search",params="_m=load")
    @ResponseBody
    public Object searchLoad(Model model, ServletRequest request) {

        Page page = super.getPage(request);

        String useStatus = request.getParameter("useStatus");
        Map<String, Object> parameters = Maps.newHashMap();

        if(StringUtils.isNotBlank(useStatus) && !useStatus.equals("-1")) {
            parameters.put("useStatus", useStatus);
        }

        page = sysDepartmentService.pageDepList(parameters, page);
        return page;
    }

    /** 获取部门列表 **/
    @RequestMapping(value = "depTree",params="_m=load")
    @ResponseBody
    public Object depTreeLoad(Model model, ServletRequest request) {

        List<Map<String, Object>> resultList = Lists.newArrayList();
        Map<String, Object> parameter = Maps.newHashMap();

        String all = request.getParameter("all");
        // 获取菜单
        List<Map<String, Object>> list = sysDepartmentService.pageDepAllList(parameter);

        // 查看是否显示全部选项
        if(all!=null && all.equals("search")) {
            setResulDep(resultList, "", "全部", true);
        }

        for(int i=list.size()-1; i>=0; i--) {
            Map<String, Object> menuTop = list.get(i);
            setResulDep(resultList, menuTop.get("uid").toString(), menuTop.get("depName").toString(), false);
        }
        return resultList;
    }

    /*****************************************************************************************/
    /** 设置下拉菜单选项 **/
    private void setResulDep(List<Map<String, Object>> resultList, String uid, String depName, boolean select) {
        Map<String, Object> resulDep = Maps.newHashMap();
        resulDep.put("uid", uid);
        resulDep.put("depName", depName);
//        if(select) {
//            resulMenu.put("selected", true);
//        }
        resultList.add(resulDep);

    }

    /*****************************************************************************************/
    /** 启用 **/
    @RequestMapping(value = "on",params="_m=exec")
    @ResponseBody
    public Object onExec(Model model, ServletRequest request) {
        return lockPower(request, "1");
    }

    /** 禁用 **/
    @RequestMapping(value = "off",params="_m=exec")
    @ResponseBody
    public Object offExec(Model model, ServletRequest request) {
        return lockPower(request, "0");
    }

    /*****************************************************************************************/
    /** 启用、禁用角色 **/
    private Object lockPower(ServletRequest request, String type) {
        Map<String, Object> parameters = Maps.newHashMap();
        Result result = isCheck(request);
        Date date = new Date();

        try {
            WebUserBean webUserBean = getWebUserBean(request);
            SysDepartmentEntity sysDepartmentEntity = sysDepartmentService.findUniqueById(getUid(request));
            sysDepartmentEntity.setUseStatus(type);
            sysDepartmentEntity.setUpdateBy(webUserBean.getUid());
            sysDepartmentEntity.setUpdateAt(date);

            sysDepartmentService.update(sysDepartmentEntity);

            result.resoutSucess("操作成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            result.resoutError("操作失败");
        }

        return result;
    }

}
